package com.ruoyi.web.controller.order;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.domain.HotProduct;
import com.ruoyi.order.domain.NewProduct;
import com.ruoyi.order.domain.SeasonProduct;
import com.ruoyi.order.domain.Vo.ProductVo;
import com.ruoyi.order.service.IHotProductService;
import com.ruoyi.order.service.INewProductService;
import com.ruoyi.order.service.ISeasonProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.Product;
import com.ruoyi.order.service.IProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品表Controller
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Controller
@RequestMapping("/order/product")
public class ProductController extends BaseController
{
    private String prefix = "order/product";

    @Autowired
    private IHotProductService hotProductService;

    @Autowired
    private IProductService productService;

    @Autowired
    private INewProductService newProductService;

    @Autowired
    private ISeasonProductService seasonProductService;

    @RequiresPermissions("order:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询产品表列表
     */
    @RequiresPermissions("order:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出产品表列表
     */
    @RequiresPermissions("order:product:export")
    @Log(title = "产品表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        return util.exportExcel(list, "产品表数据");
    }

    /**
     * 新增产品表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品表
     */
    @RequiresPermissions("order:product:add")
    @Log(title = "产品表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Product product)
    {
        product.setCreateUser(getUserId().intValue());
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改产品表
     */
    @RequiresPermissions("order:product:edit")
    @GetMapping("/edit/{productID}")
    public String edit(@PathVariable("productID") Integer productID, ModelMap mmap)
    {
        logger.debug(""+productID);
        Product product = productService.selectProductByProductID(productID);
        mmap.put("product", product);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品表
     */
    @RequiresPermissions("order:product:edit")
    @Log(title = "产品表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Product product)
    {
        product.setUpdateUser(getUserId().intValue());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品表
     */
    @RequiresPermissions("order:product:remove")
    @Log(title = "产品表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productService.deleteProductByProductIDs(ids));
    }


    /**
     * 模糊查询产品表列表
     */
    @PostMapping("/getProductPages")
    @ResponseBody
    public TableDataInfo getProductPages(String productName)
    {
        startPage();
        if(productName.equals("") || productName.length()==0){
            return getDataTable(productService.getProductPagesList());
        }
        List<ProductVo> list = productService.getProductPages(productName);
        return getDataTable(list);
    }


    /**
     * 添加热门产品表列表
     */
    @PostMapping("/setHotProduct")
    @ResponseBody
    public AjaxResult setHotProduct(Integer[] productIDs)
    {

        for (int i = 0; productIDs !=null && i < productIDs.length; i++) {
            HotProduct product = hotProductService.selectHotProductById(productIDs[i]);
            if(product!=null){
                continue;
            }
            HotProduct hotProduct = new HotProduct();
            hotProduct.setProductID(productIDs[i]);
            hotProduct.setState(0);
            hotProduct.setCreateUser(getUserId().intValue());
            hotProduct.setCreateTime(DateUtils.getNowDate());
            hotProductService.insertHotProduct(hotProduct);
        }

        return AjaxResult.success();
    }
    /**
     * 添加新型产品表列表
     */
    @PostMapping("/setNewProduct")
    @ResponseBody
    public AjaxResult setNewProduct(Integer[] productIDs)
    {
        for (int i = 0; productIDs !=null && i < productIDs.length; i++) {
            NewProduct product = newProductService.selectNewProductById(productIDs[i]);
            if(product!=null){
                continue;
            }
            NewProduct newProduct = new NewProduct();
            newProduct.setProductID(productIDs[i]);
            newProduct.setState(0);
            newProduct.setCreateUser(getUserId().intValue());
            newProduct.setCreateTime(DateUtils.getNowDate());
            newProductService.insertNewProduct(newProduct);
        }
        return AjaxResult.success();
    }
    /**
     * 添加热门产品表列表
     */
    @PostMapping("/setSeasonProduct")
    @ResponseBody
    public AjaxResult setSeasonProduct(Integer[] productIDs)
    {

        for (int i = 0; productIDs !=null && i < productIDs.length; i++) {
            SeasonProduct product = seasonProductService.selectSeasonProductById(productIDs[i]);
            if(product!=null){
                continue;
            }
            SeasonProduct seasonProduct = new SeasonProduct();
            seasonProduct.setProductID(productIDs[i]);
            seasonProduct.setState(0);
            seasonProduct.setCreateUser(getUserId().intValue());
            seasonProduct.setCreateTime(DateUtils.getNowDate());
            seasonProductService.insertSeasonProduct(seasonProduct);
        }
        return AjaxResult.success();
    }
}
